<template>
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加'+activeName:dialogType === 'detail'?activeName+'详情':'修改'+activeName"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="dataForm.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit } from '@/api/info/allInfo'

export default {
  name: 'AddAllInfoDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    activeName: {
      type: String,
      default: ''
    },
    code: {
      type: String,
      default: null
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        name: null, infoType: null
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    initData() {
      if (this.obj) {
        this.dataForm = JSON.parse(JSON.stringify(this.obj))
      }
      if (this.code) {
        this.dataForm.infoType = this.code
      }
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          if (this.dialogType === 'add') {
            add(this.dataForm).then(() => {
              this.$message.success('成功')
              this.$emit('closeDialog')
              this.$emit('flushPage', this.code)
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          } else if (this.dialogType === 'update') {
            edit(this.dataForm).then(() => {
              this.$message.success('成功')
              this.$emit('closeDialog')
              this.$emit('flushPage', this.code)
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          }
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        name: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
